package com.chijun.mform.field.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chijun.mform.R;
import com.chijun.mform.create.field.IJiSuanGongShiListener;
import com.chijun.mform.create.mode.FormField;
import com.example.ccj.b.c.recycler.BaseRecycler;
import com.example.ccj.b.c.recycler.DefaultAdapterOnBind;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.ScreenUtils;
import com.example.ccj.u.SizeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 计算公式设置类
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/26 16:24
 */
public class InstallJiSuanGongShi extends LinearLayout {


    private TextView mJp1;
    private TextView mJp2;
    private TextView mJp3;
    private TextView mJp4;
    private TextView mJp5;
    private TextView mJp6;
    private TextView mJp7;
    private TextView mJp8;
    private TextView mJp9;
    private TextView mJp0;
    private TextView mJpJia;
    private TextView mJpJian;
    private TextView mJpCheng;
    private TextView mJpChu;
    private TextView mJpZuoKuoHao;
    private TextView mJpYouKuoHao;
    private TextView mTvText;
    private TextView mTvClear;
    private BaseRecycler<FormField> formFieldRecycler;
    private List<FormField> allFormFields;
    private List<FormField> canJiSuanFormFields;
    private IJiSuanGongShiListener iJiSuanGongShiListener;
    private String s = "";



    public InstallJiSuanGongShi(Context context,String s, List<FormField> formFields, IJiSuanGongShiListener iJiSuanGongShiListener) {
        super(context);
        this.allFormFields = formFields;
        LogUtils.d("s:" + s);
        if (s == null) this.s = "";
        else this.s = s;
        canJiSuanFormFields = new ArrayList<>();
        this.iJiSuanGongShiListener = iJiSuanGongShiListener;
        for (FormField formField : formFields) {
            if (formField.getType() == FormField.JIN_E || formField.getType() == FormField.SHU_ZI) {
                canJiSuanFormFields.add(formField);
            }
        }
        init();
    }

    public InstallJiSuanGongShi(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init(){
        saveData = new StringBuilder();
        stringBuilder = new StringBuilder();
        LinearLayout.inflate(getContext(), R.layout.view_install_ji_suan_gong_shi,this);
        mJp1 = findViewById(R.id.view_install_shu_zi_jian_pan_1);
        mJp2 = findViewById(R.id.view_install_shu_zi_jian_pan_2);
        mJp3 = findViewById(R.id.view_install_shu_zi_jian_pan_3);
        mJp4 = findViewById(R.id.view_install_shu_zi_jian_pan_4);
        mJp5 = findViewById(R.id.view_install_shu_zi_jian_pan_5);
        mJp6 = findViewById(R.id.view_install_shu_zi_jian_pan_6);
        mJp7 = findViewById(R.id.view_install_shu_zi_jian_pan_7);
        mJp8 = findViewById(R.id.view_install_shu_zi_jian_pan_8);
        mJp9 = findViewById(R.id.view_install_shu_zi_jian_pan_9);
        mJp0 = findViewById(R.id.view_install_shu_zi_jian_pan_0);
        mJpJia = findViewById(R.id.view_install_shu_zi_jian_pan_jia);
        mJpJian = findViewById(R.id.view_install_shu_zi_jian_pan_jian);
        mJpCheng = findViewById(R.id.view_install_shu_zi_jian_pan_cheng);
        mJpChu = findViewById(R.id.view_install_shu_zi_jian_pan_chu);
        mJpZuoKuoHao = findViewById(R.id.view_install_shu_zi_jian_pan_left);
        mJpYouKuoHao = findViewById(R.id.view_install_shu_zi_jian_pan_right);
        mTvClear = findViewById(R.id.view_install_ji_suan_gong_shi_clear);
        mTvText = findViewById(R.id.view_install_ji_suan_gong_shi_text);
        formFieldRecycler = findViewById(R.id.view_install_ji_suan_gong_shi_rec);


        mTvText.setText(s);
        mJp1.setOnClickListener(getShuZiOnClickListener("1"));
        mJp2.setOnClickListener(getShuZiOnClickListener("2"));
        mJp3.setOnClickListener(getShuZiOnClickListener("3"));
        mJp4.setOnClickListener(getShuZiOnClickListener("4"));
        mJp5.setOnClickListener(getShuZiOnClickListener("5"));
        mJp6.setOnClickListener(getShuZiOnClickListener("6"));
        mJp7.setOnClickListener(getShuZiOnClickListener("7"));
        mJp8.setOnClickListener(getShuZiOnClickListener("8"));
        mJp9.setOnClickListener(getShuZiOnClickListener("9"));
        mJp0.setOnClickListener(getShuZiOnClickListener("0"));
        mJpJia.setOnClickListener(getShuZiOnClickListener(" + "));
        mJpJian.setOnClickListener(getShuZiOnClickListener(" - "));
        mJpCheng.setOnClickListener(getShuZiOnClickListener(" * "));
        mJpChu.setOnClickListener(getShuZiOnClickListener(" / "));
        mJpZuoKuoHao.setOnClickListener(getShuZiOnClickListener("("));
        mJpYouKuoHao.setOnClickListener(getShuZiOnClickListener(")"));
        mTvClear.setOnClickListener(v -> clear());

        int appScreenWidth = ScreenUtils.getAppScreenWidth();
        int dpWidth = SizeUtils.px2dp(appScreenWidth);
        int i = dpWidth / 80;

        formFieldRecycler.setLayoutManager(new GridLayoutManager(getContext(),i));
        formFieldRecycler.setDefaultAdapter(canJiSuanFormFields, R.layout.adapter_ji_suan_field, new DefaultAdapterOnBind<FormField>() {
            @Override
            public View onBind(View view, FormField formField) {
                Button mBtn = view.findViewById(R.id.adapter_ji_suan_field_text);
                mBtn.setText(formField.getName());
                mBtn.setOnClickListener(v -> setShowText(FIELD_TYPE,formField.getName(),formField));
                return view;
            }
        });
    }

    public static final int SHU_ZI_TYPE = 1;
    public static final int FIELD_TYPE = 2;

    StringBuilder stringBuilder;

    private View.OnClickListener getShuZiOnClickListener(String str){
        return v -> setShowText(SHU_ZI_TYPE,str,null);
    }

    StringBuilder saveData;

    private void setShowText(int type,String str,FormField formField){
        stringBuilder.append(str);
        if (type == SHU_ZI_TYPE) {
            saveData.append(str);
        } else if (type == FIELD_TYPE) {
            saveData.append("<").append(formField.getId()).append(">");
        }
        mTvText.setText(stringBuilder);
        LogUtils.d(stringBuilder);
        LogUtils.d(saveData);
        if (iJiSuanGongShiListener != null) iJiSuanGongShiListener.onResult(saveData.toString(),stringBuilder.toString());
    }

    private void clear(){
        stringBuilder = new StringBuilder();
        saveData = new StringBuilder();
        mTvText.setText("");
    }

}
